/**
 * var box =
 {
 "ID":"008",
 "Caption":"区1",
 "Ports":
 [
 {"ID":"1001","Caption":"A_1","Title":"显示名称","Status":1,"ZP":1},
 {"ID":"1002","Caption":"A_2","Title":"显示名称","Status":1,"ZP":1},
 {"ID":"1003","Caption":"A_3","Title":"显示名称","Status":2,"ZP":2},
 {"ID":"1004","Caption":"A_4","Title":"显示名称","Status":3,"ZP":1},
 {"ID":"1005","Caption":"A_5","Title":"显示名称","Status":3,"ZP":2},
 {"ID":"1006","Caption":"A_6","Title":"显示名称","Status":3,"ZP":2}
 ]
 }
 * var boxT=new Box("box5","V",3,1,0,0,true,false);
    var s = boxT.initialize(box);
     将s添加html中
     boxT.SetPortStatus(boxT,box)

 */

var ResDir="../images/";
var ItemCount=12;
var doc = document;
var SelImgID="";

var PortImgs = new Array;
var PortRemark = new Array;
PortImgs[0]= ResDir + "circle_grey.png";
PortImgs[1]= ResDir + "circle_grey.png";
PortImgs[2]= ResDir + "circle_white.png";
PortImgs[3]= ResDir + "circle_red.png";
//PortImgs[4]= ResDir + "star_yellow.png";
//PortImgs[5]= ResDir + "circle_red.png";
//PortImgs[6]= ResDir + "circle_yellow.png";
//PortImgs[7]= ResDir + "circle_green.png";

PortRemark[0]="损坏";
PortRemark[1]="未熔接";
PortRemark[2]="未跳接";
PortRemark[3]="已跳接";
PortRemark[4]="";
PortRemark[5]="";
PortRemark[6]="";
PortRemark[7]="";
function Box(id,HV,EntriesCount,HSort,VSort,Style,ShowCaption,action)
{	
	this.HV= HV;
	this.HSort=HSort;
	this.VSort=VSort;	
	this.EntriesCount=EntriesCount;
	this.ShowCaption =ShowCaption;
	this.Style=Style;
	this.id=id;
	this.action=action;
	this.CurrentPort;
	this.SelPorts="";
	this.initialize = initializeBox;
	this.setPort =setPortValue;
	this.getSelPorts=getSelPortsValue;
    this.SetPortStatus = SetPortStatus;
}


function initializeBox(box)
{	
	var s = '';
	if(this.ShowCaption)
	     if(box!=null)
			s += box.Caption+box.ID;
			s += "box_"+this.id;
			s += "<table class='box' id='box_"+this.id+"' name='box_"+this.id+"' tag='"+this.Style+"'>";		
	if(this.HV=="H")
		s += drawBox_H(this,box);
	if(this.HV=="V")
		s += drawBox_V(this,box);
	s += "</table>";
    return s;
}

function setPortValue(box)
{	
	if(box==null) return;
	SetPortStatus(this,box)
}

function getSelPortsValue()
{
	var sjson=getSelPortsValueByBoxID(this.id);
	
	return(sjson);
	
}

function getSelPortsValueByBoxID(boxid){
	var s="";
	var style="0";
	var box=doc.getElementById("box_"+boxid); 
	var selbox1="";
	var selbox2="";
	if(box)
	{
		style=box.tag;
		var chkboxs=doc.getElementsByName("okaysoft_checkbox_"+boxid);		
		if(chkboxs) 
		 	for(var i=0;i<chkboxs.length;i++) 
			{ 	
				if(chkboxs[i].checked)
				{
					if(selbox1=="")
						selbox1="{'Caption':'"+chkboxs[i].Port_Caption+"','ID':'"+chkboxs[i].Port_ID+"'}";
					else
						selbox1=selbox1+",{'Caption':'"+chkboxs[i].Port_Caption+"','ID':'"+chkboxs[i].Port_ID+"'}";
				}				
			}
		
		var radboxs=doc.getElementsByName("okaysoft_radio_"+boxid);	
		if(radboxs) 
		 	for(var i=0;i<radboxs.length;i++) 
			{ 	
				if(radboxs[i].checked)
				{
					if(selbox2=="")
						selbox2="{'Caption':'"+radboxs[i].Port_Caption+"','ID':'"+radboxs[i].Port_ID+"'}";
					else
						selbox2=selbox2+",{'Caption':'"+radboxs[i].Port_Caption+"','ID':'"+radboxs[i].Port_ID+"'}";
				}				
			}
	}
	if(style=="0")
		s="({'Style':"+style+",'Ports':[],'ZG_Ports':[],'PX_Ports':[]})";
	if(style=="1")
		s="({'Style':"+style+",'Ports':["+selbox1+"],'ZG_Ports':[],'PX_Ports':[]})";
	if(style=="2")
		s="({'Style':"+style+",'Ports':[],'ZG_Ports':["+selbox2+"],'PX_Ports':["+selbox1+"]})";
	
	var sjson=eval(s);
	return(sjson);
}

function drawItem(boxObj,id,style,action){
	boxObj.SelPorts="www.okaysoft.cn";
	var s = '';
	if(style==0)
	{
		s += "<img src='"+PortImgs[1]+"' class='img1' id='"+id+"' Port_ID='' Port_Caption='"+id+"'";
		if(action)
			s += " onClick='ItemClick1(this)'";
		s += ">";
	}
	if(style==1 ||style==2)
	{
		s += "<div class='imgbg'><img src='"+PortImgs[1]+"' class='bgimg'>";
		s += "<input type='checkbox' name='okaysoft_checkbox_"+boxObj.id+"' id='okaysoft_checkbox_"+id+"' value='"+id+"' Port_ID='' Port_Caption='"+id+"' class='cr'";
		if(action)
			s += " onClick='ItemClick2(this)'";
		s += ">";

		s += "<input type='radio' name='okaysoft_radio_"+boxObj.id+"' id='okaysoft_radio_"+id+"' value='"+id+"' Port_ID='' Port_Caption='"+id+"' class='cr' style='display:none'";
		if(action)
			s += " onClick='ItemClick2(this)'";
		s += ">";

		s += "</div>";
	}
	return s ;
}

function SetPortStatus(item,box){
    //根据端子信息初始化端子样式
	if(box==null) return;

	 for(var i=0;i<box.Ports.length;i++)
 	{
		var port=box.Ports[i];
		var PortItem;
		
		if(item.Style==0){
			PortItem=doc.getElementById(item.id+"_"+port.Caption); 
			if(PortItem)
			{	
				PortItem.Port_ID=port.ID;
				PortItem.title=port.Title;
				PortItem.src=PortImgs[port.Status];
			}		
		}

		if(item.Style==1||item.Style==2){
			PortItem=doc.getElementById("okaysoft_checkbox_"+item.id+"_"+port.Caption); 
			if(PortItem)
			{	
				PortItem.Port_ID=port.ID;
				PortItem.title=port.Title;
				var node=PortItem.parentNode.childNodes[0];
				if(node)
					node.src=PortImgs[port.Status];

                //当端子状态为损坏不可用时，禁止选中
				if(port.Status==0){					
					PortItem.style.display='none';
				}
			}
		}
					
		
		if(item.Style==2){
			PortItem=doc.getElementById("okaysoft_radio_"+item.id+"_"+port.Caption); 
			if(PortItem)
			{	
				PortItem.Port_ID=port.ID;
				PortItem.title=port.Title;
				var node=PortItem.parentNode.childNodes[0];
				if(node)
					node.src=PortImgs[port.Status];

                //当端子状态为损坏不可用时，禁止选中
				if(port.Status==0){					
					PortItem.style.display='none';
				}
			}
		}



         //设置端子主干或配线显示
		var TdItem=$(".td_"+item.id+"_"+port.Caption)//doc.getElementById("td_"+item.id+"_"+port.Caption);
		if(TdItem)
		{	if(port.ZP==0)
			{ TdItem.className='item'; }	
			if(port.ZP==1){
			     //TdItem.className= TdItem.className + ' zg';
                TdItem.addClass('zg');
			     if(item.Style==2)//选跳接配对端子时，把主干端子变为radio
			     {							
				  var objCheck=doc.getElementById("okaysoft_checkbox_"+item.id+"_"+port.Caption);
				  if(objCheck)
				       objCheck.style.display='none';
				
				  var objRadio=doc.getElementById("okaysoft_radio_"+item.id+"_"+port.Caption);
				  if(objRadio)
				       objRadio.style.display='';				  			
			     }			     
			}
			if(port.ZP==2){
                TdItem.addClass('px');
			    //TdItem.className=TdItem.className + ' px';
			}
		}
 	}
}

function drawBox_H(entries,box)
{	
	
	var s = '';
	s += "<tr height='10px'><td></td></tr>";	
	for(var i=0;i<entries.EntriesCount;i++)
	{	
		if(entries.HSort==0)
			var cap=String.fromCharCode(65+i);
		else
			var cap=String.fromCharCode(65+entries.EntriesCount-i-1);

		s += "<tr><td>";
		s += "<table class='entry'><tr class='tr1'>";
		s += "<td class='Caption'>"+cap+"</td>";
		for(var j=1;j<=ItemCount;j++)
		{
			if(entries.VSort==0)	
				var nCol=j;
			else
				var nCol=ItemCount-j+1;
			s += "<td id='td_"+entries.id+"_"+cap+"_"+nCol+"' class='item td_"+entries.id+"_"+cap+"_"+nCol+"'>";
			s += drawItem(entries,entries.id+"_"+cap+"_"+nCol,entries.Style,entries.action,entries); //��ʾ����Ĭ������			
			s += "</td>";
		}
		s += "<td class='Caption'>"+cap+"</td>";
		s += "</tr>";
		s += "<tr class='tr2'>";
		s += "<td></td>";
		for(var j=1;j<=ItemCount;j++)
		{
			s += "<td valign='top' align='center'>";
			if(entries.VSort==0)	
				s +=j;
			else
				s +=ItemCount-j+1;
			s += "</td>";
		}
		s += "<td></td></tr></table>";
		s += "</td></tr>";
	}
    return s;
	//SetPortStatus(entries,box);
}


function drawBox_V(entries,box)
{	
	var s = '';
	s += "<tr><td width='10px'></td><td></td></tr><tr><td rolspan="+ItemCount+"></td>";	
	for(var i=0;i<entries.EntriesCount;i++)
	{	
		if(entries.HSort==0)
			var cap=String.fromCharCode(65+i);
		else
			var cap=String.fromCharCode(65+entries.EntriesCount-i-1);
		s += "<td>";
		s += "<table class='entry'><tr>";
		s += "<td class='Caption' align='center'>"+cap+"</td><td></td></tr>";
		for(var j=1;j<=ItemCount;j++)
		{
			if(entries.VSort==0)	
				var nCol=j;
			else
				var nCol=ItemCount-j+1;

			s += "<tr><td class='item td_"+entries.id+"_"+cap+"_"+nCol+"' id='td_"+entries.id+"_"+cap+"_"+nCol+"'>";
				
			s += drawItem(entries,entries.id+"_"+cap+"_"+nCol,entries.Style,entries.action,entries);
	
			s += "</td>";

			s += "<td class='td2' valign='center' align='left'>";
			if(entries.VSort==0)	
				s += j;
			else
				s += ItemCount-j+1;
			s += "</td></tr>";
		}
		s += "<tr><td class='Caption' align='center'>"+cap+"</td><td></td>";
		s += "</tr>";
		
		s += "<td></td></tr></table>";
		s += "</td>";
	}
	s += "</tr>";
    return s;
	//SetPortStatus(entries,box);
}

function ItemClick1(item)
{	
	if(SelImgID!=""){
            var SelImg=doc.getElementById(SelImgID);
	    if(SelImg)
		SelImg.className='img1';	
	}
	SelImgID=item.id;
	item.className='img2';
	PortClick(item);
}


function ItemClick2(item)
{
	if(SelImgID!=""){
            var SelObj=doc.getElementById(SelImgID);
	    if(SelObj){
		var node=SelObj.parentNode.childNodes[0];
		if(node)
			node.className='bgimg1';	
	    }
	}
	SelImgID=item.id;
	var nodeT=item.parentNode.childNodes[0];
	if(nodeT)
		nodeT.className='bgimg2';	
	PortClick(item);
}

function DisplayTS()
{
    var s = '';
    //显示图示
    /*s += "<table>";
	s += "<tr><td>&nbsp;<b>图示:</b></td><td>";
	s += "<table border='0'><tr><td class='bgzg'>a</td></tr></table>";
	s += "</td><td>背景为主干端子&nbsp;&nbsp;</td><td>";
	s += "<table border='0'><tr><td class='bgpx'>b</td></tr></table>";
	s += "</td><td>背景为配线端子&nbsp;&nbsp;</td>";
	
	for(var i=0;i<PortImgs.length;i++){
		s += "<td>";
		s += "<table style='border:0px solid #fff; padding:1px;margin:1px;'><tr><td><img src='"+PortImgs[i]+"' class='ts'></td></tr></table>";
		s += "</td><td>为"+PortRemark[i]+"端子&nbsp;&nbsp;</td>";
	}
	s += "</tr>";
	s += "</td></tr></table>";*/
    s += "<div style='border: 1px solid #CAD9EA;padding: 1px'>" +
        "<div style='float:left;width:80px;'><div style='float:left;background: #00A046;width:20px;height:20px;'></div><label>主干端子</label></div>" +
        "<div style='float:left;width:80px;'><div style='float:left;background: #A04600;width:20px;height:20px;'></div><label>配线端子</label></div>";
    for(var i=0;i<PortImgs.length;i++){
        if(i!=1){
        s+="<div style='float:left;width:80px;'><img src='"+PortImgs[i]+"' class='ts'/>为"+PortRemark[i]+"端子</div>";
        }
    }
    s +=    "<div style=\"clear:both;\"></div>" +
        "</div>"
	return s;
}

(function($){

    $.fn.occ = function(options,json){
        options = options || {};
        return this.each(function(){
            var boxT=new Box(options.id,options.hv,options.num,options.caoNum,options.sort,options.layout,options.showTit,options.click);
            var s = boxT.initialize(json);

            $(this).html(DisplayTS()+s);
            boxT.SetPortStatus(boxT,json) ;
        })

    }
}) (jQuery)